﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.IO;
using System.Text.RegularExpressions;
using System.Windows.Forms;
using Microsoft.VisualBasic;

namespace KABS_Commander
{
    public partial class KABS_Commander : Form
    {

        private List<string> getCommands(string module)
        {
            List<string> result = new List<string>();
            byte[] data = new byte[1024];
            int count = GetPrivateProfileSection(module, data, 1024,
                                                 System.AppDomain.CurrentDomain.BaseDirectory + configFileName);

            if (count == 0) return result;
            decodeIniReadBytes(data, count, result);

            return result;
        }

        private void addCmdsToList(List<string> cmds)
        {
            foreach (string cmd in cmds)
            {
                listBox_cmdlist.Items.Add(cmd);
            }
        }

        private void InitializeCommand()
        {
            string module = comboBox_module.Text;
            List<string> cmds = new List<string>();

            if (module == "ALL")
            {
                ComboBox.ObjectCollection items = comboBox_module.Items;
                foreach (string s in items)
                {
                    List<string> temp = getCommands(s);
                    cmds.AddRange(temp);
                }
                button_rmModule.Enabled = false;
            }
            else
            {
                cmds = getCommands(module);
                button_rmModule.Enabled = true;
            }

            listBox_cmdlist.Items.Clear();
            addCmdsToList(cmds);
        }

        private void modifyToolStripMenuItem_Click(object sender, EventArgs e)
        {
            string path = System.AppDomain.CurrentDomain.BaseDirectory + configFileName;
            string cmd = listBox_cmdlist.Text;

            if (cmd.Length == 0) return;

            List<string> cmds = new List<string>();
            readIniLines(path, cmds);
            int index = cmds.IndexOf(cmd);
            string newcmd = Interaction.InputBox("Input new command", "Modify", cmds[index]);

            if (index == -1 || newcmd.Length == 0)
                return;

            cmds[index] = cmds[index].Replace(cmd, newcmd);

            writeIniLines(path, cmds);

            InitializeCommand();
        }

        private void modifyAndAddToolStripMenuItem_Click(object sender, EventArgs e)
        {
            string path = System.AppDomain.CurrentDomain.BaseDirectory + configFileName;
            string cmd = listBox_cmdlist.Text;

            if (cmd.Length == 0) return;

            List<string> cmds = new List<string>();
            readIniLines(path, cmds);
            int index = cmds.IndexOf(cmd);

            if (index == -1)
                return;
            string newcmd = Interaction.InputBox("Input the new command", "Modify and Add", cmds[index]);

            if (newcmd.Length == 0)
                return;
            cmds.Insert(index, newcmd);

            writeIniLines(path, cmds);

            InitializeCommand();
        }

        private void deleteToolStripMenuItem_Click(object sender, EventArgs e)
        {
            string path = System.AppDomain.CurrentDomain.BaseDirectory + configFileName;
            string cmd = listBox_cmdlist.Text;

            if (cmd.Length == 0) return;

            DialogResult res = MessageBox.Show("Remove Command [" + cmd + "]?", "WARNING", MessageBoxButtons.YesNo);

            if (res != DialogResult.Yes)
                return;

            List<string> cmds = new List<string>();
            readIniLines(path, cmds);
            cmds.Remove(cmd);
            writeIniLines(path, cmds);

            InitializeCommand();
        }

        private void addToolStripMenuItem_Click(object sender, EventArgs e)
        {
            string path = System.AppDomain.CurrentDomain.BaseDirectory + configFileName;
            string module = comboBox_module.Text;
            List<string> cmds = new List<string>();
            string newcmd = Interaction.InputBox("Input the new command", "Modify and Add", "");

            if (newcmd.Length == 0)
                return;

            WritePrivateProfileString(module, newcmd, "", path);
            removeIniEqualMark(path); // remove "="

            InitializeCommand();
        }

        // simulate a left click
        public void DoMouseClick(int x, int y)
        {
            mouse_event(MOUSEEVENTF_LEFTDOWN | MOUSEEVENTF_LEFTUP, x, y, 0, 0);
        }

        private void listBox_cmdlist_MouseDown(object sender, MouseEventArgs e)
        {
            if (e.Button == System.Windows.Forms.MouseButtons.Right)
            {
                DoMouseClick(e.X, e.Y);
                if (comboBox_module.Text == "ALL")
                {
                    contextMenuStrip_cmdlist.Items[0].Enabled = false;
                    contextMenuStrip_cmdlist.Items[2].Enabled = false;
                }
                else
                {
                    contextMenuStrip_cmdlist.Items[0].Enabled = true;
                    contextMenuStrip_cmdlist.Items[2].Enabled = true;
                }
            }
        }
    }
}